package com.sgq.vue.service.picture.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgq.vue.dao.picture.PictureMapper;
import com.sgq.vue.dao.picture.TagMapper;
import com.sgq.vue.entity.picture.Picture;
import com.sgq.vue.entity.picture.Tag;
import com.sgq.vue.service.picture.PictureService;
import com.sgq.vue.service.system.FileService;
import com.sgq.vue.vo.query.picture.PictureQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 83778
 * @date: 2024-01-04 17:40
 * @desc:
 */
@Service
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture> implements PictureService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private FileService fileService;

    /**
     * 查询所有 不限制上传人
     * @param pictureQueryVo
     * @return
     */
    @Override
    public IPage<Picture> findByPage(PictureQueryVo pictureQueryVo) {
        Long pageNo = pictureQueryVo.getPageNo();
        Long pageSize = pictureQueryVo.getPageSize();
        IPage<Picture> iPage = new Page<>(pageNo,pageSize);

        LambdaQueryWrapper<Picture> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Picture::getPlateId,pictureQueryVo.getPlateId());
        queryWrapper.like(!ObjectUtils.isEmpty(pictureQueryVo.getTags()),Picture::getTags,pictureQueryVo.getTags()+",");
        queryWrapper.like(!ObjectUtils.isEmpty(pictureQueryVo.getDescription()),Picture::getDescription,pictureQueryVo.getDescription());
        IPage<Picture> page = this.baseMapper.selectPage(iPage, queryWrapper);
        //如果标签不为空，则从中筛选
        if (!ObjectUtils.isEmpty(pictureQueryVo.getTags())){
            List<Picture> collect = page.getRecords().stream().filter(picture -> {
                String[] split = picture.getTags().split(",");
                List<String> list = Arrays.asList(split);
                return list.contains(pictureQueryVo.getTags());
            }).collect(Collectors.toList());
            page.setRecords(collect);
        }
        page.getRecords().forEach(picture -> {
            picture.getParam().put("tags",this.getTags(picture.getTags()));
        });
        return page;
    }

    @Override
    public List<Picture> findWeatherImgByTagId(Long tageId) {
        LambdaQueryWrapper<Picture> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Picture::getPlateId, 51L);
        queryWrapper.like(Picture::getTags, tageId+",");
        List<Picture> pictures = this.baseMapper.selectList(queryWrapper);
        return pictures;
    }

    @Override
    public boolean editPictureTag(Long pictureId, String tags) {
        int i = this.baseMapper.updateTagsById(pictureId, tags);
        return i == 1 ? true : false;
    }

    /**
     * 真实删除
     * @param ids
     * @return
     */
    @Override
    public Integer realRemoveByIds(List<Long> ids) {
        //1-删除数据库中picture的先关信息
        Integer integer = this.baseMapper.realDeleteByIds(ids);
        //2-删除oss中图片的信息
        //先通过id获取picture的地址
        List<String> urls = this.baseMapper.realSelectUrlByIds(ids);
        //再遍历删除
        for (String url : urls) {
            fileService.deleteFile(url);
        }
        return integer;

    }

    private List<Tag> getTags(String tags){
        String[] split = tags.split(",");
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Tag::getId,split);
        List<Tag> tagList = tagMapper.selectList(queryWrapper);
        return tagList;
    }
}
